package org.tigris.atlas.quizme.client.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.tigris.atlas.messages.Message;
import org.tigris.atlas.messages.MessageFactory;
import org.tigris.atlas.messages.MessageUtils;
import org.tigris.atlas.messages.Messages;
import org.tigris.atlas.quizme.client.form.QuestionForm;
import org.tigris.atlas.quizme.client.utils.ClientUtils;
import org.tigris.atlas.quizme.service.QuestionServiceResponse;
import org.tigris.atlas.quizme.service.QuizTopicServiceResponse;
import org.tigris.atlas.quizme.service.delegate.EntityMaintenanceServiceDelegate;
import org.tigris.atlas.quizme.service.delegate.ServiceDelegateFactory;
import org.tigris.atlas.quizme.transfer.Answer;
import org.tigris.atlas.quizme.transfer.Question;
import org.tigris.atlas.quizme.transfer.QuizTopic;
import org.tigris.atlas.quizme.transfer.QuizTopicPK;
import org.tigris.atlas.quizme.transfer.TransferObjectFactory;

/**
 * Log the user into the system.  This action accesses the business layer using
 * the "ServiceDelegatefactory" from atlas.  This abtracts the business layer 
 * so that struts does not need to know how to access the service.  It could be using
 * webservices, ejb or pojo implementation.
 * 
 * @author Robert Simmering
 */
public class SaveQuestionAction  extends Action {
	public ActionForward execute(ActionMapping mapping, ActionForm form,
		      HttpServletRequest request, HttpServletResponse response) {	
		EntityMaintenanceServiceDelegate delegate = ServiceDelegateFactory.createEntityMaintenanceServiceDelegate();
		QuestionForm questionForm = (QuestionForm)form;
		Question newQuestion = TransferObjectFactory.createQuestion();
		questionForm.copyTo(newQuestion);
		
		QuizTopicPK topicPK = TransferObjectFactory.createQuizTopicPK();
		topicPK.setTopicId(questionForm.getQuizTopicId());
		QuizTopicServiceResponse serviceResponse = delegate.findByPrimaryKey(topicPK);
		newQuestion.setQuizTopic(serviceResponse.getQuizTopic());
		
		if("on".equals(questionForm.getQuestionMultiAnswer())){
			newQuestion.setMultiAnswer(Boolean.TRUE);
		}
		else{
			newQuestion.setMultiAnswer(Boolean.FALSE);
		}
		
		int numAnswers = 0;
		if(!StringUtils.isBlank(questionForm.getAnswer1Text())){
			Answer answer = TransferObjectFactory.createAnswer();
			answer.setQuestion(newQuestion);
			answer.setOrder(new Integer(1));
			answer.setAnswer(questionForm.getAnswer1Text());
			if("on".equals(questionForm.getAnswer1Correct())){
				answer.setCorrect(Boolean.TRUE);
			}
			else{
				answer.setCorrect(Boolean.FALSE);
			}
			newQuestion.addAnswer(answer);
			numAnswers++;
		}
		
		if(!StringUtils.isBlank(questionForm.getAnswer2Text())){
			Answer answer = TransferObjectFactory.createAnswer();
			answer.setQuestion(newQuestion);
			answer.setOrder(new Integer(2));
			answer.setAnswer(questionForm.getAnswer2Text());
			if("on".equals(questionForm.getAnswer2Correct())){
				answer.setCorrect(Boolean.TRUE);
			}
			else{
				answer.setCorrect(Boolean.FALSE);
			}
			newQuestion.addAnswer(answer);
			numAnswers++;
		}
		if(!StringUtils.isBlank(questionForm.getAnswer3Text())){
			Answer answer = TransferObjectFactory.createAnswer();
			answer.setQuestion(newQuestion);
			answer.setOrder(new Integer(3));
			answer.setAnswer(questionForm.getAnswer3Text());
			if("on".equals(questionForm.getAnswer3Correct())){
				answer.setCorrect(Boolean.TRUE);
			}
			else{
				answer.setCorrect(Boolean.FALSE);
			}
			newQuestion.addAnswer(answer);
			numAnswers++;
		}
		if(!StringUtils.isBlank(questionForm.getAnswer4Text())){
			Answer answer = TransferObjectFactory.createAnswer();
			answer.setQuestion(newQuestion);
			answer.setOrder(new Integer(4));
			answer.setAnswer(questionForm.getAnswer4Text());
			if("on".equals(questionForm.getAnswer4Correct())){
				answer.setCorrect(Boolean.TRUE);
			}
			else{
				answer.setCorrect(Boolean.FALSE);
			}
			newQuestion.addAnswer(answer);
			numAnswers++;
		}
		if(!StringUtils.isBlank(questionForm.getAnswer5Text())){
			Answer answer = TransferObjectFactory.createAnswer();
			answer.setQuestion(newQuestion);
			answer.setOrder(new Integer(5));
			answer.setAnswer(questionForm.getAnswer5Text());
			if("on".equals(questionForm.getAnswer5Correct())){
				answer.setCorrect(Boolean.TRUE);
			}
			else{
				answer.setCorrect(Boolean.FALSE);
			}
			newQuestion.addAnswer(answer);
			numAnswers++;
		}
		if(!StringUtils.isBlank(questionForm.getAnswer6Text())){
			Answer answer = TransferObjectFactory.createAnswer();
			answer.setQuestion(newQuestion);
			answer.setOrder(new Integer(6));
			answer.setAnswer(questionForm.getAnswer6Text());
			if("on".equals(questionForm.getAnswer6Correct())){
				answer.setCorrect(Boolean.TRUE);
			}
			else{
				answer.setCorrect(Boolean.FALSE);
			}
			newQuestion.addAnswer(answer);
			numAnswers++;
		}
		if(!StringUtils.isBlank(questionForm.getAnswer7Text())){
			Answer answer = TransferObjectFactory.createAnswer();
			answer.setQuestion(newQuestion);
			answer.setOrder(new Integer(7));
			answer.setAnswer(questionForm.getAnswer7Text());
			if("on".equals(questionForm.getAnswer7Correct())){
				answer.setCorrect(Boolean.TRUE);
			}
			else{
				answer.setCorrect(Boolean.FALSE);
			}
			newQuestion.addAnswer(answer);
			numAnswers++;
		}
		if(!StringUtils.isBlank(questionForm.getAnswer8Text())){
			Answer answer = TransferObjectFactory.createAnswer();
			answer.setQuestion(newQuestion);
			answer.setOrder(new Integer(8));
			answer.setAnswer(questionForm.getAnswer8Text());
			if("on".equals(questionForm.getAnswer8Correct())){
				answer.setCorrect(Boolean.TRUE);
			}
			else{
				answer.setCorrect(Boolean.FALSE);
			}
			newQuestion.addAnswer(answer);
			numAnswers++;
		}
		newQuestion.setAnswerCount(new Integer(numAnswers));
		
		QuestionServiceResponse sdResponse = delegate.save(newQuestion);
		if(sdResponse.getMessages() != null && sdResponse.getMessages().hasErrorMessages()){		
			ClientUtils.getMessagesHandler().handleMessages( sdResponse.getMessages(), questionForm.getClass(), request);
			return mapping.findForward("fail");
		}
		else{
			Message msg = MessageUtils.createErrorMessage("success.addQuestion", null, null);
			Messages messages = MessageFactory.createMessages();
			messages.addMessage(msg);
			ClientUtils.getMessagesHandler().handleMessages(messages, questionForm.getClass(), request);
		}
		
		return mapping.findForward("success");
	}
}
